package com.baomidou.blog.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

	@ControllerAdvice
	static class WebMvcControllerAdvice {
		@InitBinder
		public void registerCustomEditors(WebDataBinder binder) {
			// configure for empty string change to null
			binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		}
	}

	@Autowired
	protected void configureThymeleafSpringTemplateEngine(SpringTemplateEngine templateEngine) {
		templateEngine.setEnableSpringELCompiler(true);
	}

}
